<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="1000" destroy-on-close @closed="$emit('closed')" :align-center="true">
		<el-form ref="dialogForm" :model="form" :rules="rules" label-width="110px" label-position="right">
                    <el-row :gutter="1" >
                        <el-col :span="11">
                            <el-form-item label="单据编号" prop="order">
                                <el-input v-model="form.order" placeholder="请输入零件名称" clearable autocomplete="off"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="11">
                            <el-form-item label="业务日期" prop="time">
                                <el-date-picker v-model="form.time" type="data" placeholder="业务日期" style="width:100%" />
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row :gutter="1" >
                        <el-col :span="11">
                            <el-form-item label="付款单位" prop="putUnit">
                                <el-select v-model="form.putUnit"  filterable style="width: 100%">
                                    <el-option v-for="item in cates" :key="item.id" :label="item.name" :value="item.id"/>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="11">
                            <el-form-item label="支出项目" prop="putPro">
                                <el-select v-model="form.putPro"  filterable style="width: 100%">
                                    <el-option v-for="item in cates" :key="item.id" :label="item.name" :value="item.id"/>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row :gutter="1" >
                        <el-col :span="11">
                            <el-form-item label="金额" prop="price">
                                <el-input v-model="form.price" clearable autocomplete="off"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="11">
                            <el-form-item label="支出账户" prop="putAccount">
                                <el-select v-model="form.putAccount"  filterable style="width: 100%">
                                    <el-option v-for="item in cates" :key="item.id" :label="item.name" :value="item.id"/>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row :gutter="1" >
                        <el-col :span="11">
                            <el-form-item label="经手人" prop="userName">
                                <el-select v-model="form.userName"  filterable style="width: 100%">
                                    <el-option v-for="item in cates" :key="item.id" :label="item.name" :value="item.id"/>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-col :span="22">
                        <el-form-item label="备注" prop="remark">
                            <el-input v-model="form.remark" clearable autocomplete="off"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="22">
                        <el-form-item label="关联图片" prop="img">
                            <sc-upload-multiple v-model="form.img" draggable tip="(支持: ipg jpeg png 最多上传5张)"></sc-upload-multiple>
                        </el-form-item>
                    </el-col>
                </el-form>
		<template #footer>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">确 定</el-button>
			<el-button @click="visible=false" >取 消</el-button>
		</template>
	</el-dialog>
</template>

<script>
import { defineAsyncComponent } from 'vue';
const scEditor = defineAsyncComponent(() => import('@/components/scEditor'));
	export default {
		components: {
			scEditor
		},
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增支出',
					edit: '新增支出',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
                grade:"潜力",
				//表单数据
				form: {
					order:"",
                    time:"",
                    putUnit:"",
                    putPro:"",
                    price:"",
                    putAccount:"",
                    userName:"",
                    remark:"",
                    img:""
				},
				//验证规则
				rules: {
					order: [
						{required: true, message: '请输入单据编号'}
					],
					time: [
						{required: true, message: '请选择业务日期'}
					],
                    putPro: [
						{required: true, message: '请选择支出项目'}
					],
                    price: [
						{required: true, message: '请输入金额'}
					],
                    putAccount: [
						{required: true, message: '请选择支出账户'}
					],
                    userName: [
						{required: true, message: '请选择经手人户'}
					],
			},
				cates: [],  //品牌
                Settlement:[],//结算方式
                muDan:[
                    {id:0,name:'否'},
                    {id:1,name:'是'},
                ],
			}
		},
		mounted() {
            this.getCate()
            this.getSettlement()
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res  = [];
						if (this.form.custom_id) {
							res = await this.$API.customer.getCustomerList.edit.post(this.form);
						}else{
							res = await this.$API.customer.getCustomerList.add.post(this.form);
						}
						this.isSaveing = false;
						if(res.code == 1){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}else{
						return false;
					}
				})
			},
			//表单注入数据
			setData(data){
                console.log('data :>> ', data);
                this.form.custom_id = data.custom_id
                this.form.apply_name = data.apply_name
                this.form.referred_as = data.referred_as
                this.form.linkman = data.linkman
                this.form.mobile = data.mobile
                this.form.tax_identification_no = data.tax_identification_no
                this.form.custom_delivery_time = data.custom_delivery_time
                this.form.produce_delivery_time = data.produce_delivery_time
                this.form.sell_tolerance = data.sell_tolerance
                this.form.settlement_way = data.settlement_way
                this.form.system_disable = data.system_disable
                this.form.desc = data.desc
                this.form.is_master_order_send = data.is_master_order_send
                this.form.grade = data.grade
                this.form.address = data.address
                this.form.brand_str = data.brand_arr
                this.form.remark = data.remark
                this.form.sell_ask = data.sell_ask
			},
            // 获取品牌
            async getCate(){
				var res = await this.$API.customer.getBrand.lists.get();
				this.cates = res.data.rows;
			},
            // 获取结算方式
            async getSettlement(){
				var res = await this.$API.customer.getSettlement.get();
				this.Settlement = res.data;
			},
		}
	}
</script>

<style lang="scss">
.el-form-item--default{
    align-items: center;
}
</style>
